package demo01;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class MiaoKillDemo {
    static ArrayList<Goods> arrayList = new ArrayList<>();

    public static void main(String[] args) throws ParseException {
        //获取信息
        getGoods(arrayList);
        //展示商品剩余信息
        showGoods(arrayList);
        //秒杀
        miaoKill(arrayList);

    }

    //创建获取商品秒杀时间
    public static void getGoods(ArrayList<Goods> arrayList) {
        //创建商品对象  属性有name   price
        Goods goods1 = new Goods("1.高邮咸鸭蛋", 22.8, 50);  //第二份减少5元
        Goods goods2 = new Goods("2.艾美特白天鹅", 649, 10);
        Goods goods3 = new Goods("3.瓢虫项链", 599, 5);
        Goods goods4 = new Goods("4.OPPO手机", 2799, 3);
        arrayList.add(goods1);
        arrayList.add(goods2);
        arrayList.add(goods3);
        arrayList.add(goods4);
    }

    public static void showGoods(ArrayList<Goods> arrayList) {
        System.out.println("目前商品信息：");
        arrayList.forEach(goods -> System.out.println(goods));
    }

    public static void miaoKill(ArrayList<Goods> arrayList) throws ParseException {
        LocalTime g1StarTime = LocalTime.of(10, 30, 00);
        LocalTime g2StarTime = LocalTime.of(16, 30, 00);
        LocalTime g3StarTime = LocalTime.of(18, 30, 00);
        LocalTime g4StarTime = LocalTime.of(20, 30, 00);


        LocalTime g1EndTime = LocalTime.of(14, 30, 00);
        LocalTime g2EndTime = LocalTime.of(16, 30, 00);
        LocalTime g3EndTime = LocalTime.of(18, 30, 00);
        LocalTime g4EndTime = LocalTime.of(20, 30, 00);


        LocalTime nowTime = LocalTime.now();




/*

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");  //创建格式化时间对象

        //格式化对象解析字符串  并转为毫秒值
        long starTime1 = sdf.parse(g1StarTime).getTime();
        System.out.println(starTime1);
        long starTime2 = sdf.parse(g2StarTime).getTime();
        long starTime3 = sdf.parse(g3StarTime).getTime();
        long starTime4 = sdf.parse(g4StarTime).getTime();
        long endTime1 = sdf.parse(g1EndTime).getTime();
        long endTime2 = sdf.parse(g2EndTime).getTime();
        long endTime3 = sdf.parse(g3EndTime).getTime();
        long endTime4 = sdf.parse(g4EndTime).getTime();
*/

        while (true) {
            System.out.println("请输入您需要秒杀的商品序号：");
            Scanner scanner = new Scanner(System.in);
            int chooseNumer = scanner.nextInt();
            if (chooseNumer == 1) {
                //判断时间和数量是否合法
                if ((nowTime.isAfter(g1StarTime) && nowTime.isBefore(g1EndTime)) && arrayList.get(0).getNumer() > 0) {
                    arrayList.get(0).setNumer(arrayList.get(0).getNumer() - 1);
                    System.out.println("秒杀成功");
                } else {
                    System.out.println("您输入的商品不存在或不在秒杀时间内。");
                }
                break;
            } else if (chooseNumer == 2) {
                if ((nowTime.isAfter(g2StarTime) && nowTime.isBefore(g2EndTime)) && arrayList.get(1).getNumer() > 0) {
                    arrayList.get(1).setNumer(arrayList.get(1).getNumer() - 1);
                    System.out.println("秒杀成功");
                } else {
                    System.out.println("您输入的商品不存在或不在秒杀时间内。");
                }
                break;
            } else if (chooseNumer == 3) {
                if ((nowTime.isAfter(g3StarTime) && nowTime.isBefore(g3EndTime)) && arrayList.get(2).getNumer() > 0) {
                    arrayList.get(2).setNumer(arrayList.get(2).getNumer() - 1);
                    System.out.println("秒杀成功");
                } else {
                    System.out.println("您输入的商品不存在或不在秒杀时间内。");
                }
                break;
            } else if (chooseNumer == 4) {
                if ((nowTime.isAfter(g4StarTime) && nowTime.isBefore(g4EndTime)) && arrayList.get(3).getNumer() > 0) {
                    arrayList.get(3).setNumer(arrayList.get(3).getNumer() - 1);
                    System.out.println("秒杀成功");
                } else {
                    System.out.println("您输入的商品不存在或不在秒杀时间内。");
                }
                break;
            } else {
                System.out.println("您输入的序号有误，请重新输入：");
            }
        }
        showGoods(arrayList);

        while (true) {
            System.out.println("是否继续参与秒杀？（输入“Y”确定继续，否则按任意键退出）");
            Scanner scanner = new Scanner(System.in);
            String inPut = scanner.next();
            if (inPut.equals("Y")) {
                miaoKill(arrayList);
            } else {
                break;
            }
        }
    }
}
